package com.project.service.impl;

import com.project.bean.ProductBean;
import com.project.bean.ShopBean;
import com.project.mapper.IProductMapper;
import com.project.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 采购/销售车业务接口实现类
 */
@Service
@Transactional
public class ShopServiceImpl implements IShopService {
    @Autowired
    private IProductMapper productMapper;
    @Override
    public void buy(String option, ProductBean product, Integer buyNum, Map<String, ShopBean> buyMap) {
        if("add".equals(option)){//添加操作，向库存表中添加记录
            productMapper.insert(product);
        }

        //判断采购车是否有该商品
        if(buyMap.containsKey(product.getId()) == false){
            ShopBean shopBean = new ShopBean();
            shopBean.setProduct(product);
            shopBean.setNumber(buyNum);
            buyMap.put(product.getId(),shopBean);
        }
        else{
            ShopBean shopBean = buyMap.get(product.getId());
            shopBean.setNumber(shopBean.getNumber() + buyNum);
        }
    }

    @Override
    public void remove(String productId, Map<String, ShopBean> shopMap) {
        shopMap.remove(productId);
    }

    @Override
    public void updateBuyInfo(String productId, Integer buyPrice, Integer buyNum, Map<String, ShopBean> buyMap) {
        ShopBean shopBean = buyMap.get(productId);
        shopBean.setPrice(buyPrice);
        shopBean.setNumber(buyNum);
    }

    @Override
    public void sell(String productId, Map<String, ShopBean> sellMap) {
        if(sellMap.containsKey(productId) == false){
            ShopBean shopBean = new ShopBean();
            ProductBean product = productMapper.selectById(productId);
            shopBean.setProduct(product);
            shopBean.setNumber(1);
            shopBean.setPrice(product.getSellPrice());
            sellMap.put(productId,shopBean);
        }
        else{
            ShopBean shopBean = sellMap.get(productId);
            shopBean.setNumber(shopBean.getNumber() + 1);
        }
    }

    @Override
    public void updateSellInfo(String productId, Integer sellNum, Map<String, ShopBean> sellMap) {
        ShopBean shopBean = sellMap.get(productId);
        shopBean.setNumber(sellNum);
    }
}
